home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / moves.arc / MOVES.ASM < prev    next >
Assembly Source File  |  1991-01-09  |  4KB  |  104 lines

  1. PAGE 80,132
  2. TITLE TP 4.0 16-bit Move procedures
  3.  
  4. ; MOVES.ASM - Move16, Move16n
  5. ; Makes a copy of one array of bytes to another.  Overlapping is accounted for
  6. ; for.  Both routines are about twice as fast the original TP move, because
  7. ; 16-bit transfers are used in lieu of the 8-bit.
  8.  
  9. ; Move16 is a suitable replacement for TP move and is only 11 bytes more.
  10. ; Just like TP move, it is used for INTRAsegment moves.
  11.  
  12. ; Move16n is needed for special applications for Pointers such as the heap
  13. ; when you need to make absolutely sure there is no overlap.  It is used for
  14. ; either INTER- or INTRA-segment moves.  The "n" stands for normalize which
  15. ; always makes the offset less than 16.  Because it normalizes the pointers,
  16. ; it takes a little longer to get started.
  17.  
  18. ;   by James H. LeMay, CIS 76011,217
  19.  
  20. CODE   SEGMENT WORD PUBLIC
  21.        ASSUME  CS:CODE
  22.        PUBLIC  Move16, Move16n
  23.  
  24. ; procedure Move16 (VAR Source,Dest; NumOfBytes: word);
  25. ; Parameters:  Source,Dest - Double word pointers
  26. ;              NumOfBytes  - Number of bytes to move
  27.  
  28. Source      EQU   DWORD PTR [bp+0Ch]
  29. Dest        EQU   DWORD PTR [bp+08h]
  30. NumOfBytes  EQU   WORD  PTR [bp+06h]
  31.  
  32. Move16       PROC FAR
  33.        push  bp                ; Save Turbo's BP
  34.        mov   bp,sp             ; Set up stack frame
  35.        mov   dx,ds             ; Save Turbo's DS
  36.        lds   si,Source         ; Load pointer
  37.        les   di,Dest           ; Load pointer
  38.        mov   cx,NumOfBytes     ; Move count
  39. Equal: cmp   si,di             ; Overlap?
  40.        jb    Rev               ;   yes, reverse direction
  41. Fwd:   cld                     ;   no, direction forward
  42.        shr   cx,1              ; Bytes -> words
  43.        jnc   Word2             ; Even number of bytes?
  44.        movsb                   ;   no, move first one
  45.        jmp   SHORT Word2       ; Now do words
  46. Rev:   std                     ;   yes, reverse direction
  47.        add   si,cx             ; adjust source
  48.        dec   si                ;
  49.        add   di,cx             ; adjust dest
  50.        dec   di                ;
  51.        shr   cx,1              ; Bytes -> words
  52.        jnc   Word1             ; Even number of bytes?
  53.        movsb                   ;   no, move first one
  54. Word1: dec   si                ; Adjust source
  55.        dec   di                ; Adjust dest
  56. Word2: rep   movsw             ; Move 'em!
  57.        mov   ds,dx             ; Restore Turbo's DS
  58.        pop   bp                ; Restore Turbo's BP
  59.        ret   10                ; Return to call
  60. Move16       ENDP
  61.  
  62.  
  63. ; procedure Move16n (VAR Source,Dest; NumOfBytes: word);
  64. ; Parameters:  Source,Dest - Double word pointers
  65. ;              NumOfBytes  - Number of bytes to move
  66.  
  67. Mask15      EQU   00001111b
  68.  
  69. Normalize    PROC NEAR
  70.        mov   ax,es             ; Copy seg
  71.        mov   bx,di             ; Copy offset
  72.        mov   cl,4              ; Set counter
  73.        shr   bx,cl             ; Divide by 16
  74.        add   ax,bx             ; Add to segment
  75.        and   di,Mask15         ; Remainder in seg
  76.        ret                     ; Return to call
  77. Normalize    ENDP
  78.  
  79. Move16n      PROC FAR
  80.        push  bp                ; Save Turbo's BP
  81.        mov   bp,sp             ; Set up stack frame
  82.        mov   dx,ds             ; Save Turbo's DS
  83.        ; -- Load and normalize Source pointer --
  84.        les   di,Source         ; Load pointer temporarily
  85.        call  normalize         ; Normalize pointer
  86.        mov   ds,ax             ; copy new Source seg
  87.        mov   si,di             ; copy new Source ofs
  88.        ; -- Load and normalize Dest pointer --
  89.        les   di,Dest           ; Load pointer
  90.        call  normalize         ; Normalize pointer
  91.        mov   es,ax             ; copy new Dest seg
  92.        mov   cx,NumOfBytes     ; Move count
  93.        ; -- Now compare segments --
  94.        mov   bx,ds             ; Copy Source seg in BX
  95.        cmp   ax,bx             ; Overlap?
  96.        je    Equal             ;   maybe
  97.        ja    Fwd               ;   no, do move
  98.        jmp   SHORT Rev         ;   yes, reverse direction
  99. Move16n      ENDP
  100.  
  101. CODE   ENDS
  102.  
  103.        END
  104.